from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from processes.models import *
from myapp.models import *
from tools.jwt_decode import login
# Create your views here.
# 查看，创建流程
class CreateProcessAPIView(APIView):
    # 查看流程
    def get(self,request):
        process_all = ProcessModel.objects.all()
        temp = []
        for i in process_all:
            temp.append({
                'id':i.id,
                'process':i.process
            })
        return Response({
            'code':200,
            'msg':'流程查询成功',
            'data':temp
        })

    # 创建流程
    @login
    def post(self,request):
        uid = request.uid
        user = UserModel.objects.get(id=uid)
        if user.role_id_id != 2:
            return Response({
                'code':400,
                'msg':'抱歉，您没有权限创建'
            })
        process = request.data.get('process')

        pro_all = ProcessModel.objects.filter(process=process).first()
        if pro_all:
            return Response({
                'code':400,
                'msg':'已经有该流程'
            })
        ProcessModel.objects.create(process=process)
        return Response({
            'code':200,
            'msg':'流程创建成功'
        })


# 节点创建，查看
class CreatePointAPIView(APIView):
    # 节点创建
    @login
    def post(self,request):
        uid = request.uid
        user = UserModel.objects.get(id=uid)
        if user.role_id_id != 2:
            return Response({
                'code': 400,
                'msg': '抱歉，您没有权限创建'
            })
        point = request.data.get('point')
        process_id = request.data.get('process_id')
        for i,j in point.items():
            PointModel.objects.create(point=j,process_id_id=process_id)
        return Response({
            'code':200,
            'msg':'节点添加成功'
        })

    @login
    def get(self,request):
        process_id = request.data.get('process_id')

        point_all = PointModel.objects.filter(process_id_id=process_id)
        temp = []
        for i in point_all:
            temp.append({
                'id':i.id,
                'point':i.point,
                'process':i.process_id.process
            })
        return Response({
            'code':200,
            'msg':'获取节点成功',
            'data':temp
        })

# 节点查看单个
class CreatePointView(APIView):
    # 查看单个
    def get(self,request,id):
        point_all = PointModel.objects.filter(process_id_id=id)
        temp = []
        for i in point_all:
            temp.append({
                'id': i.id,
                'point': i.point,
                'process': i.process_id.process
            })
        return Response({
            'code': 200,
            'msg': '获取节点成功',
            'data': temp
        })

# 节点属性创建，查看
class AttrAPIView(APIView):
    # 样式属性查看
    def get(self,request):
        attr_all = AttrModel.objects.all()
        temp = []
        for i in attr_all:
            temp.append({
                'id':i.id,
                'attr':i.attr,
                'type':i.type_id.type
            })
        return Response({
            'code':200,
            'msg':'获取属性成功',
            'data':temp
        })


# 节点样式创建
class PointStyleAPIView(APIView):
    # 节点样式查看

    # 节点样式创建
    @login
    def post(self,request):
        process_id = request.data.get('process_id')

        point_id = request.data.get('point_id')
        style_list = request.data.get('style_list')
        print(">>>>>>>>>>>",style_list)

        for i,j in style_list.items():
            PointLookModel.objects.create(process_id_id=process_id,point_id_id=point_id,attr_id_id=j)
        return Response({
            'code':200,
            'msg':'节点样式创建成功'
        })

# 节点样式查看单个
class PointStyleView(APIView):
    # 查看单个节点样式
    def get(self,request,id):
        point_one = PointModel.objects.filter(process_id_id=id).first().id
        point_style = PointLookModel.objects.filter(point_id_id=point_one)
        temp = []
        for i in point_style:
            temp.append({
                'id':i.id,
                'point':i.point_id.point,
                'attr':i.attr_id.attr,
                'way':i.attr_id.type_id.way
            })
        return Response({
            'code':200,
            'msg':'获取单个节点样式成功',
            'data':temp
        })



# 员工申请流程
class ApplyProcessAPIView(APIView):
    # 员工查看流程
    @login
    def get(self,request):
        """
        process_instance_id
        username
        point_id
        act_time
        actor
        point_state
        process_state
        comments

        """
        uid = request.uid
        user = UserModel.objects.get(id=uid).username
        user_list = UserModel.objects.get(id=uid)
        self_list = ProcessAprovalModel.objects.filter(username=user).filter(process_state='审批中')
        process_self = []
        for i in self_list:
            process_self.append({
                'id':i.id,
                'process_instance_id':i.process_instance_id.process,
                'username':i.username,
                'point_id':i.point_id.point,
                'actor':i.actor.username,
                'point_state':i.point_state,
                'process_state':i.process_state
            })

        process_other = []
        if user_list.rank_id_id == 4:
            apro_list = ProcessAprovalModel.objects.filter(point_state='未审批')
            for i in apro_list:
                process_other.append({
                    'id':i.id,
                    'process_instance_id':i.process_instance_id.process,
                    'username':i.username,
                    'point_id':i.point_id.point,
                    'actor':i.actor.username,
                    'point_state':i.point_state,
                    'process_state':i.process_state
                })
        if user_list.rank_id_id == 3:
            apro_list = ProcessAprovalModel.objects.filter(point_state='经理已审批')
            for i in apro_list:
                process_other.append({
                    'id':i.id,
                    'process_instance_id':i.process_instance_id.process,
                    'username':i.username,
                    'point_id':i.point_id.point,
                    'actor':i.actor.username,
                    'point_state':i.point_state,
                    'process_state':i.process_state
                })
        if user_list.rank_id_id == 2:
            apro_list = ProcessAprovalModel.objects.filter(point_state='主管已审批')
            for i in apro_list:
                process_other.append({
                    'id':i.id,
                    'process_instance_id':i.process_instance_id.process,
                    'username':i.username,
                    'point_id':i.point_id.point,
                    'actor':i.actor.username,
                    'point_state':i.point_state,
                    'process_state':i.process_state
                })
        print(">>>>>>>>>",process_self)
        return Response({
            'code':200,
            'msg':'获取审批信息完成',
            'process_self':process_self,
            'process_other':process_other
        })


    # 创建流程
    @login
    def post(self,request):
        uid = request.uid
        user = UserModel.objects.get(id=uid)
        form = request.data.get('form')
        process_id = request.data.get('process_id')
        process = ProcessModel.objects.get(id=process_id)
        point_id = PointModel.objects.filter(process_id_id=process_id).first()
        for i,j in form.items():
            PointAttrModel.objects.create(process_id_id=process_id,point_id_id=point_id.id,user_id_id=uid,attr=i,instance=j)

        """
        process_instance_id
        point_id
        act_time
        actor
        point_state
        process_state
        comments
        """
        ProcessAprovalModel.objects.create(process_instance_id_id=process.id,username=user.username,point_id_id=point_id.id,point_state='未审批',process_state='审批中')
        return Response({
            'code':200,
            'msg':'申请成功'
        })


# 审批样式查看单个
class StyleOneAPIView(APIView):
    # 查看单个
        def get(self, request, id):
            aproval = ProcessAprovalModel.objects.get(id=id)
            point_one = PointModel.objects.filter(process_id_id=aproval.process_instance_id_id)

            temp = []
            if aproval.point_state == '未审批':
                point = point_one.get(point='经理审批').id
                point_style = PointLookModel.objects.filter(point_id_id=point)
                for i in point_style:
                    temp.append({
                        'id': i.id,
                        'attr': i.attr_id.attr,
                        'way':i.attr_id.type_id.way
                    })
            elif aproval.point_state == '经理已审批':
                point = point_one.get(point='主管审批').id
                point_style = PointLookModel.objects.filter(point_id_id=point)
                for i in point_style:
                    temp.append({
                        'id': i.id,
                        'attr': i.attr_id.attr,
                        'way':i.attr_id.type_id.way
                    })
            elif aproval.point_state == '主管已审批':
                point = point_one.get(point='总监审批').id
                point_style = PointLookModel.objects.filter(point_id_id=point)
                for i in point_style:
                    temp.append({
                        'id': i.id,
                        'attr': i.attr_id.attr,
                        'way':i.attr_id.type_id.way
                    })
            return Response({
                'code': 200,
                'msg': '获取单个节点样式成功',
                'data': temp
            })

# 样式审批实例添加
class StyleAPIView(APIView):
    # 样式添加
    @login
    def post(self,request):
        uid = request.uid
        form = request.data.get('form')
        username_id = request.data.get('username_id')
        user = UserModel.objects.get(id=uid)
        appr_one = ProcessAprovalModel.objects.get(id=username_id).process_instance_id_id
        point_one = PointModel.objects.filter(process_id_id=appr_one)
        if user.rank_id_id == 4:
            point_id = point_one.get(point='经理审批').id
            look_list = PointLookModel.objects.filter(point_id_id=point_id).first()
            for i,j in form.items():
                PointAttrModel.objects.create(process_id_id=look_list.process_id_id,point_id_id=look_list.point_id_id,user_id_id=uid,attr=i,instance=j)
            if form['是否同意'] == '1':
                ProcessAprovalModel.objects.filter(id=username_id).update(actor_id=uid,point_state='经理已审批',process_state='审批中')
            else:
                ProcessAprovalModel.objects.filter(id=username_id).update(actor_id=uid, point_state='经理已审批', process_state='已驳回')
        if user.rank_id_id == 3:
            point_id = point_one.get(point='主管审批').id
            look_list = PointLookModel.objects.filter(point_id_id=point_id).first()
            for i,j in form.items():
                PointAttrModel.objects.create(process_id_id=look_list.process_id_id,point_id_id=look_list.point_id_id,user_id_id=uid,attr=i,instance=j)

            if form['是否同意'] == '1':
                ProcessAprovalModel.objects.filter(id=username_id).update(actor_id=uid,point_state='主管已审批',process_state='审批中')
            else:
                ProcessAprovalModel.objects.filter(id=username_id).update(actor_id=uid, point_state='主管已审批', process_state='已驳回')

        if user.rank_id_id == 2:
            point_id = point_one.get(point='总监审批').id
            look_list = PointLookModel.objects.filter(point_id_id=point_id).first()
            for i,j in form.items():
                PointAttrModel.objects.create(process_id_id=look_list.process_id_id,point_id_id=look_list.point_id_id,user_id_id=uid,attr=i,instance=j)

            if form['是否同意'] == '1':
                ProcessAprovalModel.objects.filter(id=username_id).update(actor_id=uid,point_state='总监已审批',process_state='已通过')
            else:
                ProcessAprovalModel.objects.filter(id=username_id).update(actor_id=uid, point_state='总监已审批', process_state='已驳回')

        return Response({
            'code':200,
            'msg':'审批实例添加成功'
        })
